﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class ThemeDAO
    {
        public static List<ThemeDTO> getAll()
        {
            List<ThemeDTO> result = new List<ThemeDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.THEMEs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                ThemeDTO dto = new ThemeDTO();

                dto.MaTheme = ds.MaTheme;
                dto.TenTheme = ds.TenTheme;
                dto.Color = ds.Color;
                dto.Background = ds.Background;
                dto.Border = ds.Border;
                dto.Css = ds.CSS;

                result.Add(dto);
            }

            return result;
        }

        public static ThemeDTO getAlongCode(int maTheme)
        {
            ThemeDTO result = new ThemeDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.THEMEs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaTheme == maTheme).Single();

            result.MaTheme = query.MaTheme;
            result.TenTheme = query.TenTheme;
            result.Color = query.Color;
            result.Background = query.Background;
            result.Border = query.Border;
            result.Css = query.CSS;

            return result;
        }

        public static void Insert(ThemeDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            THEME theme = new THEME();
            theme.MaTheme = _insert.MaTheme;
            theme.TenTheme = _insert.TenTheme;
            theme.Color = _insert.Color;
            theme.Background = _insert.Background;
            theme.Border = _insert.Border;
            theme.CSS = _insert.Css;
            theme.Deleted = _insert.Delete;

            dbRaoVat.THEMEs.InsertOnSubmit(theme);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maTheme)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            THEME _delete = (from n in dbRaoVat.THEMEs
                                 where n.MaTheme == maTheme && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(ThemeDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            THEME _update = (from n in dbRaoVat.THEMEs
                             where n.MaTheme == dto.MaTheme && n.Deleted == 0
                             select n).Single();

            _update.MaTheme = dto.MaTheme;
            _update.TenTheme = dto.TenTheme;
            _update.Color = dto.Color;
            _update.Background = dto.Background;
            _update.Border = dto.Border;
            _update.CSS = dto.Css;            

            dbRaoVat.SubmitChanges();
        }
    }
}
